/**

This file is part of MaCI/GIMnet.

MaCI/GIMnet is free software: you can redistribute it and/or modify it 
under the terms of the GNU Lesser General Public License as published 
by the Free Software Foundation, either version 3 of the License, or 
(at your option) any later version.

MaCI/GIMnet is distributed in the hope that it will be useful, but WITHOUT 
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public 
License for more details.

You should have received a copy of the GNU Lesser General Public 
License along with GIMnet. (See COPYING.LESSER) If not, see 
<http://www.gnu.org/licenses/>.

**/
#ifndef MACIGUI2_H_
#define MACIGUI2_H_

#include "MaCIGUIPanel.h"
#include "DisplayCanvas.h"
#include "MachineUnit.h"
#include <wx/frame.h>
#include "Component.hpp"
#include "MaCICtrlClient.hpp"

BEGIN_DECLARE_EVENT_TYPES()
DECLARE_EVENT_TYPE(MY_EVT_SERVICE_ASSIGNMENT, -1)
END_DECLARE_EVENT_TYPES()

namespace MaCIGUI
{

  struct TAssignmentEvent
  {
    wxString name;
    int index;
    void *data;
    bool deviceRefresh; //if true, only refresh device list, otherwise this event tells that new machine is added
  };
  
  class CMaCIGUI2 : public wxFrame , public Gimbo::CComponent
  {
    DECLARE_EVENT_TABLE()
    public:
      CMaCIGUI2();

      ~CMaCIGUI2();

      /** Gimbo component functions **/
      void OnServiceAssignment(const Gimbo::TServiceAssignmentList &list);

      
      void OnConnect(gimi::GIMI *g);
      
      /** thread entry point */
      static void* Entry(void *param);

      /** event handlers */
      void OnSelectionOnDevice(wxCommandEvent &e);
      void OnPOV(wxCommandEvent &e);
      void OnMode(wxCommandEvent &e);

      void OnFollow(wxCommandEvent &e);
      
      void OnGridChecked(wxCommandEvent &e);
      void OnGridSizeChange(wxCommandEvent &e);

      void OnSelectUnit(wxCommandEvent &e);
      void OnSelectDevice(wxCommandEvent &e);

      void OnLoadMap(wxCommandEvent &e);
      void OnLoadTerrain(wxCommandEvent &e);

      void OnChildFocus(wxChildFocusEvent &e);
    
      void OnKillFocus(wxFocusEvent &e);
    
      //handlers for custom events
      void OnServiceAssignmentWx(wxCommandEvent& event);
      
    private:
      ownThreadHandle thread;
      ownMutexHandle unitsMutex;
      bool go; //thread continue value
      bool connected; //is gimi connected
      bool followMode;

      std::set<std::string> groupNames;
      
      MaCIGUIPanel panel;
      CDisplayCanvas canvas;
      std::vector<CMachineUnit*> units;
      unsigned int machineUnitsCount;
      CMachineUnit* lastSelectedUnit;
      CDeviceLayer* lastDeviceOnSelection;

      MaCI::MaCICtrl::CMaCICtrlClient *MaCICtrl;
      
      void initMachineUnits();
      void updateMachineUnits();
      bool isAcceptedByMe(const Gimbo::TServiceAssignment *si);

      void refreshDevicesList();
      
  };

}

#endif /*MACIGUI2_H_*/
